package com.testing.login;

import com.alibaba.fastjson.JSONObject;
import com.testing.sqlTest.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/5/21 20:04
 * @Created by 特斯汀Roy
 */
@WebServlet(name = "Userinfo")
public class Userinfo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String result = "{";
        String status = "1";
        String msg = "恭喜您登录成功";

        //如果获取到是空，说明没登录
        if (request.getSession().getAttribute("user") == null) {
            status = "1001";
            msg = "您没有登录，不能查询";
            result += "\"status\":\"" + status + "\"," + "\"msg\":\"" + msg + "\"}";
        } else {
            MysqlUtils mu = new MysqlUtils();
            List<Map<String, String>> userinfolist = mu.querySql("select * from userinfo where username='" + request.getSession().getAttribute("user") + "'");
            Map<String, String> userinfo = userinfolist.get(0);
            status = "1000";
            msg = "查询成功";
            JSONObject userjson=new JSONObject();
            userjson.put("data",userinfo);
            userjson.put("status",status);
            userjson.put("msg",msg);
            result=userjson.toJSONString();
        }
        response.getWriter().append(result);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
